package com.metacube.metaconnect.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.metacube.metaconnect.Service.PostService;

/**
 * Servlet implementation class postStatusServlet
 */
public class postStatusServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public postStatusServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processrequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processrequest(request, response);
	}

	/**
	 * insert the status posted by user to posts table in database
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void processrequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String post = request.getParameter("postArea");

		if (post == null)
			response.sendRedirect("index.jsp");
		else {
			if (request.getSession().getAttribute("emailId") != null) {
				String email = request.getSession().getAttribute("emailId")
						.toString();
				if (!post.equals("What You Are Working on????....")
						&& !post.trim().equals(""))
					new PostService().postStatus(email, post);

				response.sendRedirect("home.jsp");
			} else
				response.sendRedirect("index.jsp");

		}

	}
}
